/**
 * Software License, Version 1.0
 * 
 * Copyright 2003 The Trustees of Indiana University.  All rights reserved.
 * 
 *
 *Redistribution and use in source and binary forms, with or without 
 *modification, are permitted provided that the following conditions are met:
 *
 *1) All redistributions of source code must retain the above copyright notice,
 * the list of authors in the original source code, this list of conditions and
 * the disclaimer listed in this license;
 *2) All redistributions in binary form must reproduce the above copyright 
 * notice, this list of conditions and the disclaimer listed in this license in
 * the documentation and/or other materials provided with the distribution;
 *3) Any documentation included with all redistributions must include the 
 * following acknowledgement:
 *
 *"This product includes software developed by the Community Grids Lab. For 
 * further information contact the Community Grids Lab at 
 * http://communitygrids.iu.edu/."
 *
 * Alternatively, this acknowledgement may appear in the software itself, and 
 * wherever such third-party acknowledgments normally appear.
 * 
 *4) The name Indiana University or Community Grids Lab or NaradaBrokering, 
 * shall not be used to endorse or promote products derived from this software 
 * without prior written permission from Indiana University.  For written 
 * permission, please contact the Advanced Research and Technology Institute 
 * ("ARTI") at 351 West 10th Street, Indianapolis, Indiana 46202.
 *5) Products derived from this software may not be called NaradaBrokering, 
 * nor may Indiana University or Community Grids Lab or NaradaBrokering appear
 * in their name, without prior written permission of ARTI.
 * 
 *
 * Indiana University provides no reassurances that the source code provided 
 * does not infringe the patent or any other intellectual property rights of 
 * any other entity.  Indiana University disclaims any liability to any 
 * recipient for claims brought by any other entity based on infringement of 
 * intellectual property rights or otherwise.  
 *
 *LICENSEE UNDERSTANDS THAT SOFTWARE IS PROVIDED "AS IS" FOR WHICH NO 
 *WARRANTIES AS TO CAPABILITIES OR ACCURACY ARE MADE. INDIANA UNIVERSITY GIVES
 *NO WARRANTIES AND MAKES NO REPRESENTATION THAT SOFTWARE IS FREE OF 
 *INFRINGEMENT OF THIRD PARTY PATENT, COPYRIGHT, OR OTHER PROPRIETARY RIGHTS. 
 *INDIANA UNIVERSITY MAKES NO WARRANTIES THAT SOFTWARE IS FREE FROM "BUGS", 
 *"VIRUSES", "TROJAN HORSES", "TRAP DOORS", "WORMS", OR OTHER HARMFUL CODE.  
 *LICENSEE ASSUMES THE ENTIRE RISK AS TO THE PERFORMANCE OF SOFTWARE AND/OR 
 *ASSOCIATED MATERIALS, AND TO THE PERFORMANCE AND VALIDITY OF INFORMATION 
 *GENERATED USING SOFTWARE.
 */
package cgl.narada.wsinfra.wsr.storage.impl;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.util.Calendar;

import javax.xml.namespace.QName;

import cgl.narada.wsinfra.schemas.soap.EnvelopeDocument;
import cgl.narada.wsinfra.util.MarshallingUtils;
import cgl.narada.wsinfra.wsr.WsrConstants;
import cgl.narada.wsinfra.wsr.storage.WsrStorageWidget;
  
/**   
 * @author sima patel
 * $Date: 2005/04/15 15:22:34 $
 * $Revision: 1.5 $
 * The WSR storage widget encapsulates the WSR message that was either
   sent or received. The storage widget contains other information which is
   useful for querying purposes. Finally,  the widget also keeps track of 
   whether a message was acknowledged or if an acknowledgement was sent for
   the message in question.
 */

public class WsrStorageWidgetImpl implements WsrStorageWidget {
 
    private String groupId;
    private long sequenceNumber;
    private Calendar messageExpiryTime;
    private String replyPattern;
    private String replyTo;
    private byte messageStatus;
    private QName messageFault = null;
    private long retransmissionTime;
    private byte numberOfRetries;
    private EnvelopeDocument envelopeDocument;
    private MarshallingUtils marshaller = MarshallingUtils.getInstance();
    
    private String moduleName = "WsrStorageWidgetImpl: ";
    
    public WsrStorageWidgetImpl(){}
    
    public WsrStorageWidgetImpl(String groupId, long sequenceNumber){
        setGroupId(groupId);
        setSequenceNumber(sequenceNumber);
    }
    public WsrStorageWidgetImpl(String groupId, long sequenceNumber,  
                                Calendar messageExpiryTime, 
                                String replyPattern, 
                                String replyTo, byte messageStatus, 
                                QName messageFault,
                                long retransmissionTime,byte numberOfRetries,
                                EnvelopeDocument envelopeDocument) {
        setGroupId(groupId);
        setSequenceNumber(sequenceNumber);
        setMessageExpiryTime(messageExpiryTime);
        setReplyPattern(replyPattern);
        setReplyTo(replyTo);
        setMessageStatus(messageStatus);
        setMessageFault(messageFault);
        setRetransmissionTime(retransmissionTime);
        setNumberOfRetries(numberOfRetries);
        setEnvelope(envelopeDocument);
    }
    public WsrStorageWidgetImpl(String groupId, long sequenceNumber,  
                                Calendar messageExpiryTime, String replyPattern, 
                                String replyTo, byte messageStatus, 
                                QName messageFault, long retransmissionTime,
								byte numberOfRetries,
                                byte[] marshalledBytesOfEnvelopeDocument) {
        setGroupId(groupId);
        setSequenceNumber(sequenceNumber);
        setMessageExpiryTime(messageExpiryTime);
        setReplyPattern(replyPattern);
        setReplyTo(replyTo);
        setMessageStatus(messageStatus);
        setMessageFault(messageFault);
        setRetransmissionTime(retransmissionTime);
        setNumberOfRetries(numberOfRetries);
        ByteArrayInputStream baInputStream = 
                        new ByteArrayInputStream(marshalledBytesOfEnvelopeDocument);
        DataInputStream din = new DataInputStream(baInputStream);
        try {
            envelopeDocument = marshaller.readEnvelopeFromStream(din);
        } catch (Exception ioe) {
            System.out.println(moduleName + "Error while un-marshalling" + ioe);
            ioe.printStackTrace();
        }/** end try-catch block*/
    }
  
    /** Get the Group Id associated with the messsage */
    public String getGroupId(){
        return groupId;
    }
    /** sets the Group Id associated with the messsage */
    public void setGroupId(String groupId){
        this.groupId = groupId;
    }
    
    /** Get the Sequence Number associated with the messsage */
    public long getSequenceNumber(){
        return sequenceNumber;
    }  
  
    /** Sets the Sequence Number associated with the messsage */
    public void setSequenceNumber(long sequenceNumber){
        this.sequenceNumber = sequenceNumber;
    }  
    /** Get the Expiry Time associated with the messsage */
    public Calendar getMessageExpiryTime(){
        return messageExpiryTime;
    }
    /** sets the Expiry Time associated with the messsage */
    public void setMessageExpiryTime(Calendar messageExpiryTime){
        this.messageExpiryTime = messageExpiryTime;
    }
    /** Get the ReplyPattern associated with the messsage */
    public String getReplyPattern(){
        return replyPattern;
    }
    /** sets the ReplyPattern associated with the messsage */
    public void setReplyPattern(String replyPattern){
        this.replyPattern = replyPattern;
    }
  
    /** Get the ReplyTo associated with the messsage */
    public String getReplyTo(){
        return replyTo;
    }
    /** sets the ReplyTo associated with the messsage */
    public void setReplyTo(String replyTo){
        this.replyTo = replyTo;
    }
    /** Get the Status associated with the messsage */
    public byte getMessageStatus(){
        return messageStatus;
    }
    /** sets the Status associated with the messsage */
    public void setMessageStatus(byte messageStatus){
        this.messageStatus = messageStatus;
    }
    /** Retrieves the message Fault*/
    public QName getMessageFault(){
        return messageFault;
    }
    /** sets message Fault*/
    public void setMessageFault(QName messageFault){
        if(messageFault != null){
            this.messageFault = messageFault;
            setMessageStatus(WsrConstants.MESSAGE_FAULTED);
        }
    }    
    /** Sets the RetransmissionTime of the message */
    public void setRetransmissionTime(long retransmissionTime){
        this.retransmissionTime = retransmissionTime;
    }
    /** gets the retransmissionTime associated with message */
    public long getRetransmissionTime(){
        return retransmissionTime;
    }    
    /** Sets the Number Of Retries associated with Retransimission of Message */
    public void setNumberOfRetries(byte numberOfRetries){
        this.numberOfRetries = numberOfRetries;
    }
    /** gets the Number Of Retries associated with Retransmission of message */
    public byte getNumberOfRetries(){
        return numberOfRetries;
    }
    /** Retrieves the EnvelopeDocument associated with message*/
    public EnvelopeDocument getEnvelope(){
        return envelopeDocument;
    }
    /** sets EnvelopeDocument associated with message*/
    public void setEnvelope(EnvelopeDocument envelopeDocument){
        this.envelopeDocument = envelopeDocument;
    }
    /** Gets a serialized representation of the EnvelopeDocument */
    public byte[] getBytesOfEnvelopeDocument() {
        ByteArrayOutputStream baOutputStream = new ByteArrayOutputStream();
        DataOutputStream dout = new DataOutputStream(baOutputStream);
        byte[] marshalledBytesOfEnvelopeDocument= null;
        try {
            marshaller.writeEnvelopeToStream(envelopeDocument, dout);
            marshalledBytesOfEnvelopeDocument = baOutputStream.toByteArray();
        } catch (Exception ioe) {
            System.out.println(moduleName + "Error while marshalling" + ioe);
        } /** end try-catch block*/
        return marshalledBytesOfEnvelopeDocument;
    }           
}